//
//  JKCommentComposeViewController.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/12.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit
import JKAlertX

class JKCommentComposeViewController: JKBaseViewController {
    
    @IBOutlet weak var textView: JKTextView!
    
    @IBOutlet weak var textViewTopCons: NSLayoutConstraint!
    @IBOutlet weak var toolbar: UIToolbar!
    
    @IBOutlet weak var toolbarBottomCons: NSLayoutConstraint!
    
    // MARK:- 生命周期方法
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        textView.becomeFirstResponder()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        textView.resignFirstResponder()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        textView.backgroundColor = JKGlobalBgColor
        textView.placeHolderLabel.text = "不友善言论会被删除，深度讨论会被优先展示。"
        textView.jk_adapt_iOS_11_scrollView()
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: UIBarButtonItemStyle.plain, target: self, action: #selector(sendBtnClick))
        
        textViewTopCons.constant = JKNavBarHeight
        
        weak var weakSelf = self
        
        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil, queue: nil) { (noti) in
            
            // 1.获取键盘的frame
            let rect = (noti.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
            
            let bottomInset = (rect.origin.y == JKScreenH) ? JKCurrentHomeIndicatorHeight : rect.height + JKBarHeight
            
            weakSelf?.textView.contentInset = UIEdgeInsetsMake(0, 0, bottomInset, 0)
            
            if #available(iOS 11.0, *) {
                
                weakSelf?.textView.scrollIndicatorInsets = (rect.origin.y == JKScreenH) ? UIEdgeInsets.zero : UIEdgeInsetsMake(0, 0, rect.height + JKBarHeight - JKCurrentHomeIndicatorHeight, 0)
                
            } else {
                
                weakSelf?.textView.scrollIndicatorInsets = weakSelf?.textView.contentInset ?? UIEdgeInsets.zero
            }
            
            weakSelf?.toolbar.isHidden = (rect.origin.y == JKScreenH)
            weakSelf?.toolbarBottomCons.constant = JKScreenH - rect.origin.y
            
            UIView.animate(withDuration: 0.25, animations: {
                UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: 7)!)
                
                weakSelf?.view.layoutIfNeeded()
            })
        }
    }
    
    /** 发布 */
    @objc private func sendBtnClick() {
        
        let msg = textView.hasText ? "抱歉，其实您还未真正登录！" : "请输入点评内容"
        
        let _ = JKAlertView(title: "提示", message: msg, style: JKAlertStylePlain).addAction(JKAlertAction(title: "确定", style: JKAlertActionStyleDefault, handler: { (action) in
            
            
        }))?.show()
    }
    
    override func navBackButtonClick() {
        
        dismiss(animated: true, completion: nil)
    }

    
    @IBAction func hide(_ sender: UIBarButtonItem) {
        
        JKHideKeyBoard()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK: - 更新皮肤

extension JKCommentComposeViewController {
    
    override func updateSkin() {
        
        if vcSkinModel == currentSkinModel {
            return
        }
        
        super.updateSkin()
        
        if self.currentSkinModel == JKNightSkinModelValue { // 夜间模式
            
            self.textView.changeToNightSkin()
            self.toolbar.barTintColor = JKNightSkinBgColor
            
        }else{ // 日间模式
            
            self.textView.changeToDaySkin()
            self.toolbar.barTintColor = JKGlobalBgColor
        }
    }
}
